﻿using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using Treasland.MassiveGridMap.VO;

namespace Treasland.MassiveGridMap.Editor {
    public class LayerNode : Node{

        public Node parentNode;

        private float minWidth = 200.0f;
        private float minHeight = 100.0f;
        private Rect boxRect;

        private GUIStyle style;
        private GUIStyle selectedStyle;
        private Action<LayerNode> onConnectedCallback;
        private Action<LayerNode> onDeletedCallback;

        private LayerNodeVO layerNodeVO;


        public LayerNode(Vector2 _position, 
                         Node _parentNode,
                         GUIStyle _layerNodeStyle,
                         GUIStyle _selectedStyle,
                         Action<LayerNode> _onConnectedCallback,
                         Action<LayerNode> _onDeletedCallback,
                         enNodeType _type = enNodeType.LayerNode) : base(_type) {

            layerNodeVO = new LayerNodeVO();
            parentNode = _parentNode;
            style = _layerNodeStyle;
            selectedStyle = _selectedStyle;
            onConnectedCallback = _onConnectedCallback;
            onDeletedCallback = _onDeletedCallback;

            rect = new Rect(_position.x, _position.y, minWidth, minHeight);
            boxRect = new Rect(0, 0, minWidth, minHeight);
        }

        public override bool ProcessEvent(Event e) {
            base.ProcessEvent(e);

            if(e.type == EventType.MouseDown) {
                if (e.button == 1) {
                    if (rect.Contains(e.mousePosition)) {
                        // Create layer node menu

                        return true;
                    }
                } else if (e.button == 0 && rect.Contains(e.mousePosition)) {
                    if (nodeDragState != enNodeDragState.Lock) {
                        nodeDragState = enNodeDragState.ReadyDrag;
                        return true;
                    }
                }
            }

            if(e.type == EventType.MouseDrag) {
                if(nodeDragState == enNodeDragState.ReadyDrag) {
                    base.Drag(e.delta);
                    return true;
                }
            }

            return false;
        }

        public override void Draw() {

            GUI.BeginGroup(rect);
            GUI.Box(boxRect, "", style);

            GUI.EndGroup();
        }

    }
}
